package com.okq.dao;

import com.okq.dao.base.Impl.BaseDao;
import com.okq.entity.Menu;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.*;
import java.util.Map.Entry;

/**
 * 描述 : 菜单Dao
 * 作者 : zdl
 * 日期 : 2017/6/28 13:45
 */
@Repository
public class MenuDao extends BaseDao {

    public Menu getMenuById(String id) {
        return (Menu) super.get(Menu.class, id);
    }

    public List<Menu> listAllMenu() {
        String hql = "FROM Menu";
        return super.find(hql);
    }

    /**
     * 根据角色Id查询菜单
     *
     * @param roleId 角色Id
     * @return 菜单list
     */
    public List<Menu> listMenuByRoleId(String roleId) {
        String hql = "FROM Menu WHERE id IN (SELECT menuId FROM RoleMenu WHERE roleId = ?1 ) ORDER BY orderIndex ASC";
        Map<String, Object> param = new HashMap<>();
        param.put("1", roleId);
        return super.find(hql, param);
    }

    /**
     * 根据角色ID查询角色菜单
     *
     * @param roleId 角色Id
     * @return 菜单Map
     */
    public Map<String, Menu> mapMenuByRoleId(String roleId) {
        // 角色菜单
        String hql = "FROM Menu m WHERE m.id IN (SELECT rm.menuId FROM RoleMenu rm WHERE rm.roleId=?1 ) ORDER BY m.orderIndex ASC";
        Map<String, Object> param = new HashMap<>();
        param.put("1", roleId);
        List<Menu> rmList = super.find(hql, param);
        // 所有菜单
        List<Menu> mList = listAllMenu();

        Map<String, Menu> rmMap = new TreeMap<>();
        if (rmList != null) {
            for (Menu menu : rmList) {
                String menuId = menu.getId();
                rmMap.put(menuId, menu);
            }
        }

        Map<String, Menu> mMap = new TreeMap<>();
        if (mList != null) {
            for (Menu m : mList) {
                String mId = m.getId();
                if (rmMap.get(mId) != null) {
                    m.setIsSelected(true);
                } else {
                    m.setIsSelected(false);
                }
                mMap.put(mId, m);
            }
        }
        return mMap;
    }

    /**
     * 获取菜单级别Map
     *
     * @param menuMap 菜单Map
     * @param level   级别
     * @return 过滤后的菜单Map
     */
    public Map<String, Menu> mapMenuByLevel(Map<String, Menu> menuMap, Integer level) {
        Map<String, Menu> menuLevelMap = new TreeMap<>();
        if (menuMap != null) {
            // 根据级别过滤menuMap
            Set<Entry<String, Menu>> menuMapSet = menuMap.entrySet();
            for (Entry<String, Menu> entry : menuMapSet) {
                String menuId = entry.getKey();
                Menu menu = entry.getValue();
                if (level.equals(findMenuLevel(menu, 1, menuMap))) {
                    menuLevelMap.put(menuId, menu);
                }
            }
        }
        return menuLevelMap;
    }

    /**
     * 获取菜单级别
     *
     * @param menu    菜单
     * @param level   级别
     * @param menuMap 菜单Map
     * @return 级别
     */
    private Integer findMenuLevel(Menu menu, Integer level, Map<String, Menu> menuMap) {

        String parentId = menu.getParentId();
        if (StringUtils.isNotBlank(parentId)) {
            Object obj = menuMap.get(parentId);
            if (obj != null) {
                Menu m = (Menu) obj;
                return findMenuLevel(m, level + 1, menuMap);
            }
        } else {
            level = -1;
        }
        return level;
    }

}
